package nero.yncc.com.createart.activity;

import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import cn.finalteam.galleryfinal.GalleryFinal;
import cn.finalteam.galleryfinal.model.PhotoInfo;
import nero.yncc.com.createart.R;
import nero.yncc.com.createart.base.BaseActivity;
import nero.yncc.com.createart.base.Constants;
import nero.yncc.com.createart.bean.UserBean;
import nero.yncc.com.createart.utils.DialogUtil;
import nero.yncc.com.createart.utils.HttpUtils;
import nero.yncc.com.createart.utils.ImageUtils;
import nero.yncc.com.createart.utils.PopUtils;
import nero.yncc.com.createart.utils.TextUtil;
import nero.yncc.com.createart.utils.TimeUtils;
import nero.yncc.com.createart.utils.UserDataUtil;

/**
 * Created by nero on 17-6-23.
 */

@ContentView(R.layout.activity_user_change)
public class ActivityUserChange extends BaseActivity {
    @ViewInject(R.id.tv_userid)
    TextView tv_userid;

    @ViewInject(R.id.et_nickname)
    EditText et_nickname;
    @ViewInject(R.id.tv_birthday)
    TextView tv_birthday;
    @ViewInject(R.id.iv_head)
    ImageView iv_head;
    @ViewInject(R.id.tv_head)
    TextView tv_head;
    @ViewInject(R.id.rb_man)
    RadioButton rb_man;
    @ViewInject(R.id.rb_woman)
    RadioButton rb_woman;
    @Override
    protected void initView() {
        super.initView();
        setTitleBarTitle("个人信息");
        setRight("保存", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogUtil.getConfirmDialog(mContext, "是否保存个人资料？", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        saveUserData();
                    }
                }).show();
            }
        });
        initMsg();

    }

    String nickName="";
    String sex="";
    String birthday="";

    private void saveUserData() {
        nickName=et_nickname.getText().toString();
        if(rb_woman.isChecked()){
            sex="2";
        }else{
            sex="1";
        }
        birthday=tv_birthday.getText().toString();
        if(TextUtils.isEmpty(nickName)){
            showToast("请输入昵称");
            return;
        }
        if(TextUtils.isEmpty(birthday)){
            showToast("请选择生日");
            return;
        }
        HashMap<String,String> hashMap=new HashMap<>();
        hashMap.put("birthday",birthday);
        hashMap.put("nickname",nickName);
        hashMap.put("gender",sex);
        hashMap.put("user_id",userBean.getUser_id());

      showProgress("正在保存用户资料");
        new HttpUtils().post(this, Constants.API_UPDATE_USER, hashMap, UserBean.class, HttpUtils.TYPE_OBJECT, new HttpUtils.HttpCallback() {
            @Override
            public void success(String msg, Object json) {
                UserBean userBean=(UserBean)json;
                UserDataUtil.saveUserData(mContext,userBean);
                showToast(msg);
                hideProgress();
            }

            @Override
            public void fail(String failMsg) {
                showToast(failMsg);
                hideProgress();
            }
        });


    }

    private void initMsg(){
//        ImageUtils.BindImage(mContext,iv_head,userBean.getHead_pic(),1);
        x.image().bind(iv_head,Constants.IMAGE_HOST+userBean.getHead_pic(),Constants.IMAGEOPTIONS);
        tv_userid.setText(userBean.getUser_id());
        et_nickname.setText(TextUtil.getText(userBean.getNickname(),""));
        et_nickname.setSelection(et_nickname.getText().length());
        if(userBean.getSex().equals("1")){
            rb_man.setChecked(true);
        }else{
            rb_woman.setChecked(true);
        }
        tv_birthday.setText(TimeUtils.getDate(userBean.getBirthday()));
//        tv_account.setText(userBean.get);
    }

    @Override
    protected void initListener() {
        super.initListener();
        iv_head.setOnClickListener(this);
        tv_head.setOnClickListener(this);
        tv_birthday.setOnClickListener(this);
    }

    @Override
    protected void doClick(int id) {
        super.doClick(id);
        switch (id){
            case R.id.tv_head:
                chooseHead();
                break;
            case R.id.iv_head:
                chooseHead();
                break;
            case R.id.tv_birthday:
                TimePickerView pvTime = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {//选中事件回调
                        tv_birthday.setText(TimeUtils.getDate(date));
                    }
                }).setType(new boolean[]{true, true, true, false, false, false}).setDate(Calendar.getInstance())
                        .build();
                pvTime.show();
                break;
        }
    }

    private void chooseHead(){
        PopUtils.showPhotoPopwindow(mContext, true, 1, new GalleryFinal.OnHanlderResultCallback() {
            @Override
            public void onHanlderSuccess(int reqeustCode, List<PhotoInfo> resultList) {
                HashMap<String,String> hashMap=new HashMap();
                hashMap.put("user_id",userBean.getUser_id());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showProgress();
                    }
                });

                HttpUtils.uploadFileInThreadByOkHttp(ActivityUserChange.this, Constants.API_USERHEAD_CHANGE, hashMap, resultList, new HttpUtils.HttpCallback() {
                    @Override
                    public void success(String msg, Object json) {
                        userBean.setHead_pic(json.toString().replace("http://lmrz.ynthgy.com",""));
                        UserDataUtil.saveUserData(mContext,userBean);
                        showToast("修改成功");
                        x.image().bind(iv_head,Constants.IMAGE_HOST+userBean.getHead_pic(),Constants.IMAGEOPTIONS);
                        hideProgress();
                    }

                    @Override
                    public void fail(String failMsg) {
                        showToast(failMsg);
                        hideProgress();
                    }
                });
            }

            @Override
            public void onHanlderFailure(int requestCode, String errorMsg) {

            }
        });
    }

}
